//
//  CoreAnimationViewController.swift
//  DemoUI
//  coreAnimation 动画
//  Created by 白瑾浩 on 2022/1/11.
//

import UIKit

class CoreAnimationViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = .white
        //锚点x,y取值范围在0~1之前
        //渐变动画
        let gradientLayer = CAGradientLayer()
        gradientLayer.bounds = CGRect(x: 0, y: 0, width: 100, height: 100)
        gradientLayer.position = CGPoint(x: 100, y: 100)
        gradientLayer.colors = [UIColor.red.cgColor,UIColor.green.cgColor,UIColor.blue.cgColor]
            
        gradientLayer.locations = [0.2,NSNumber(value: 0.5),NSNumber(value: 0.7)]
        
        gradientLayer.startPoint = CGPoint(x: 0, y: 0)
        gradientLayer.endPoint = CGPoint(x: 1, y: 1)
        
        self.view.layer.addSublayer( gradientLayer )
        
        //拷贝图片容器
        let mCAReplicatorLayer = CAReplicatorLayer()
        mCAReplicatorLayer.position = CGPoint.zero
        
        //创建内容图层
        let subLayer = CALayer()
        subLayer.bounds = CGRect(x: 0, y: 0, width: 20, height: 20)
        subLayer.position = CGPoint(x: 30, y: 200)
        subLayer.backgroundColor = UIColor.red.cgColor
        
        
        mCAReplicatorLayer.addSublayer( subLayer )
        //设置每次拷贝将副本沿x轴平移30个像素
        mCAReplicatorLayer.instanceTransform = CATransform3DMakeTranslation(30, 0, 0)
        //设置拷贝副本的个数
        
        mCAReplicatorLayer.instanceCount = 10
        
        self.view.layer.addSublayer( mCAReplicatorLayer )
        
        //自定义形状布局画图
        let shapeLayer = CAShapeLayer()
        
        shapeLayer.position = CGPoint.zero //容器位置
        
        let path = CGMutablePath()
        path.move(to: CGPoint(x:100,y:300))
        path.addLine(to: CGPoint(x: 300, y: 300))
        path.addLine(to: CGPoint(x: 200, y: 400))
        path.addLine(to: CGPoint(x: 100, y: 300))
        
        shapeLayer.path = path
        
        shapeLayer.strokeStart = 0
        
        shapeLayer.strokeEnd  = 0.5 //三角形的边缘线条 周长倍数
        
        shapeLayer.fillRule = CAShapeLayerFillRule.evenOdd
        shapeLayer.fillColor = UIColor.red.cgColor
        shapeLayer.strokeColor = UIColor.blue.cgColor
        shapeLayer.lineWidth = 3
        
        self.view.layer.addSublayer( shapeLayer )
        
        //coreanimation的属性动画介绍
        //基础
//        let basicAni = CABasicAnimation(keyPath: "transform.rotation.z")
//        basicAni.fromValue = 0
//        basicAni.toValue = Double.pi
//
//        basicAni.duration = 3
//
//        self.view.layer.add( basicAni,forKey: nil )
//
        //帧动画
//        let keyFrameAni = CAKeyframeAnimation(keyPath: "transform.rotation.z")
//        keyFrameAni.values = [0,Double.pi/4,0,Double.pi]
//
//        keyFrameAni.duration = 3
//        self.view.layer.add( keyFrameAni, forKey: nil)
//
//
      //阻尼动画
        let springAni = CASpringAnimation(keyPath: "position.y")
        springAni.mass = 2
        springAni.stiffness = 5
        springAni.damping = 2
        springAni.toValue = 700
        springAni.duration = 5
        
        let layer = CALayer()
        layer.position = CGPoint(x: 100, y: 400)
        layer.bounds = CGRect(x: 0, y: 0, width: 50, height: 50)
        layer.backgroundColor = UIColor.red.cgColor
        self.view.layer.addSublayer( layer )
        
        layer.add( springAni, forKey: "")
        // Do any additional setup after loading the view.
        
       
        
        
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        //转场动画和组合动画
        
        let mTransition = CATransition()
        
        mTransition.type = CATransitionType.push
        mTransition.subtype = CATransitionSubtype.fromLeft
        //布局
        let mlayer = CALayer()
        mlayer.position = CGPoint(x: 100, y: 600)
        mlayer.bounds =  CGRect(x: 0, y: 0, width: 50, height: 50)
        mlayer.backgroundColor = UIColor.green.cgColor
        mlayer.add( mTransition , forKey: "")
        
        
        self.view.layer.addSublayer( mlayer )
        
        let basicAni = CABasicAnimation(keyPath: "backgroundColor")
        
        basicAni.toValue = UIColor.green.cgColor
        
        let basicAni2 = CABasicAnimation(keyPath: "transform.scale.x")
        basicAni2.toValue = 2
        
        //进行组合动画
        
        let groupAni = CAAnimationGroup()
        groupAni.animations = [basicAni,basicAni2]
        basicAni.duration = 3
        
        let layer2 = CALayer()
        layer2.position = CGPoint(x: 10, y: 300)
        layer2.bounds = CGRect(x: 0, y: 0, width: 200, height: 200)
        layer2.backgroundColor = UIColor.blue.cgColor
        layer2.add(groupAni, forKey: nil)
        
        self.view.layer.addSublayer( layer2 )
        
        

    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
